/* $$Id: RssFeedEdit.java 33 2008-01-10 16:17:18Z qxodream $$
 *
 * RssReaderMidlet --  RSS reader for J2ME(midp)
 * The original codes of this porgram is from
 * RssOwlMIDlet(zhengyun@gmail.com)
 *
 * Copyright (c) 2007-2008 QXO(qxodream@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.ultrapower.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.ultrapower.control.GUIController;
import com.ultrapower.model.RssFeed;

/**
 * 类说明：编辑选定的Rss Feed的详细信息,包括URL链接以及名称。提供“返回”和“保存”按钮。
 *
 * @author zhengyun@ultrapower 2005.10.07
 * @author QXO(qxodream@gmail.com)
 *
 */
public class RssFeedEdit extends Form {
	private GUIController controller;

	// private static Image iconEdit = SResources.getImage(
	// SResources.IMG_EDITFEED);
	private Command BACK_COMMAND;
	private Command SAVE_COMMAND;
	// private String currentFeedId;
	private TextField titleField;
	private TextField urlField;
	private RssFeed feed;

	public RssFeedEdit(String title, GUIController control) {
		super(title);
		controller = control;
		this.setCommandListener(new RssFeedEditListener());

		BACK_COMMAND = new Command("返回", Command.BACK, 1);
		SAVE_COMMAND = new Command("保存", Command.OK, 2);
		this.addCommand(BACK_COMMAND);
		this.addCommand(SAVE_COMMAND);

		titleField = new TextField("名称", "", 35, TextField.ANY);
		urlField = new TextField("Rss链接", "http://", 64, TextField.URL);

		this.append(titleField);
		this.append(urlField);
	}

	public void setRssFeed(RssFeed feed) {
		this.feed = feed;
		// this.currentFeedId = String.valueOf(feed.getId());
		titleField.setString(feed.getName());
		urlField.setString(feed.getUrl());
	}

	public void clear() {
		titleField.setString("");
		urlField.setString("http://");
	}

	/*
	 * 内部监听器，监听器监听所有Command事件，并把事件响应推出来让控制器处理
	 */
	private class RssFeedEditListener implements CommandListener {
		public void commandAction(Command command, Displayable disp) {
			if (command == BACK_COMMAND) {
				controller.handleEvent(GUIController.EventID.EVENT_EDIT_BACK,
						null);
			} else if (command == SAVE_COMMAND) {
				String title = titleField.getString();
				String url = urlField.getString();
				if ((title == null || title.equals(""))
						|| (url == null || url.equals(""))) {
					return;
				}

				feed.setName(title);
				feed.setUrl(url);
				// 传进去的参数就是feed自身,只不过将用户输入的名称以及链接更新了
				Object[] args = { feed };
				controller.handleEvent(GUIController.EventID.EVENT_EDIT_SAVE,
						args);
			}// end else
		}
	}// end inner class

}
